﻿using System;
using System.Collections.Generic;
using System.Text;
using AutoMapper;
using EfConsole.Entities;
using Newtonsoft.Json;

namespace EfConsole
{
    public class Mapper : AutoMapper.Profile
    {
        public Mapper()
        {
            CreateMap<Profile, ProfileEntity>()
                .ForMember(d => d.Content, s => s.MapFrom(i => JsonConvert.SerializeObject(i.Content)));

            CreateMap<ProfileEntity, Profile>()
                .ForMember(d => d.Content, s => s.MapFrom(i => ConvertContent(i)));
        }

        private AbstractProfileContent ConvertContent(ProfileEntity entity)
        {

            switch (entity.ContentType)
            {
                case ProfileContentType.ExportLocation:
                    return JsonConvert.DeserializeObject<ExportLocationProfileContent>(entity.Content) as AbstractProfileContent;
                default: return null;
            }
        }
    }
}
